package Chapter01.Homework;

import java.sql.*;
import java.util.Scanner;

//3:在控制台输入一个学生的全部信息，保存到数据库，
//注意日期的处理(PreparedStatement)
public class Work3 {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String url="jdbc:mysql://localhost:3306/kgc76?useSSL=false";
        Connection connection=null;
        PreparedStatement ps=null;
        try {
            connection= DriverManager.getConnection(url,"root","123456");
            System.out.println(connection.getClass().getName());
            System.out.println("连接已经建立");
            Scanner input=new Scanner(System.in);
            System.out.println("请输入学生姓名：");
            String sname=input.next();
            System.out.println("请输入性别:");
            String sex=input.next();
            System.out.println("请输入qq号：");
            String qq=input.next();
            System.out.println("请输入微信号：");
            String wechat=input.next();
            System.out.println("请输入班级号：");
            int gradeid=input.nextInt();
            System.out.println("请输入入学日期：");
            String enterdate=input.next();
            System.out.println("请输入城市：");
            String fromcity=input.next();
            System.out.println("请输入出生日期：");
            String birthday=input.next();
            System.out.println("请输入地址：");
            String address=input.next();
            System.out.println("请输入密码：");
            String pass=input.next();
            System.out.println("请输入身份证号：");
            String idcard=input.next();
            String sql="insert into `s_student`(`sname`,`sex`,`qq`,`wechat`,`gradeid`,`enterdate`,`fromcity`,`birthday`,`address`,`pass`,`idcardpic`)values(?,?,?,?,?,?,?,?,?,?,?)";
            System.out.println(sql);
            ps=connection.prepareStatement(sql);
            //!!给占位符赋值
            ps.setString(1,sname);
            ps.setString(2,sex);
            ps.setString(3,qq);
            ps.setString(4,wechat);
            ps.setInt(5,gradeid);
            ps.setString(6,enterdate);
            ps.setString(7,fromcity);
            ps.setString(8,birthday);
            ps.setString(9,address);
            ps.setString(10,pass);
            ps.setString(11,idcard);
            int zhi=ps.executeUpdate();
            System.out.printf("%d行被修改了",zhi);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                ps.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
